package com.gdcp.android_project_1;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private Button btnNext;
    private ImageView image_boys;
    private int image[] = {R.drawable.boys1, R.drawable.boys2,
            R.drawable.boys3, R.drawable.boys6,
            R.drawable.boys5,};
    int temp = 0;
    private Handler handler;
    private final int MSG_GHANGE_BOY = 10010;
    private Boolean isPause = true;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if (message.what == MSG_GHANGE_BOY) {
                    temp = (temp + 1) % image.length;
                    image_boys.setImageResource(image[temp]);
                    return true;
                }
                return false;
            }
        });
        textView = findViewById(R.id.text);

        textView.setTextSize(33);
        textView.setText(R.string.app_name1);
        textView.setTextColor(Color.BLACK);

        btnNext = findViewById(R.id.image_next);
        image_boys = findViewById(R.id.image_boy);

        btnNext.setOnClickListener(
                new View.OnClickListener() {
                    public void onClick(View view) {
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                while (true) {
                                    try {
                                        Thread.sleep(2000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    if (!isPause) {

                                        Message message = new Message();
                                        message.what = MSG_GHANGE_BOY;
                                        handler.sendMessage(message);
                                        Log.e("MainActity", "send change message");
                                    }
                                }
                            }
                        }).start();
                        btnNext.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (isPause) {
                                    isPause = false;
                                } else {
                                    isPause = true;
                                }
                            }
                        });


                    }
                }
        );
    }
}
